package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.*;
import com.qfedu.openplateform.manager.service.AppInfoService;
import com.qfedu.openplateform.manager.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 孙晨杨
 * @Date: 2021/10/27/19:19
 * @Description:
 */
@RestController
@RequestMapping("/sys/app_info")
public class AppInfoController {

    @Autowired
    private AppInfoService appInfoService;

    @Autowired
    private CustomerService customerService;

    @RequestMapping("/table")
    public TableData<AppInfo> findAll(int page, int limit) {
        //开启分页
        PageHelper.startPage(page, limit);
        //调用方法
        List<AppInfo> appInfos = appInfoService.findAll();
        // 得到分页相关信息
        PageInfo pageInfo = new PageInfo(appInfos);

        TableData<AppInfo> tableData = new TableData<>();
        tableData.setCode(0);
        tableData.setMsg("success");
        tableData.setCount((int) pageInfo.getTotal());
        tableData.setData(appInfos);

        return tableData;
    }

    // 添加应用信息
    @RequestMapping("/add")
    public AjaxMessage add(AppInfo appInfo) {
        try {
            Customer customer = customerService.getCustomerById(appInfo.getCusId());

            appInfo.setCorpName(customer.getNickname());
            appInfoService.insert(appInfo);

            return new AjaxMessage(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(false, "添加失败");
    }

    //删除
    @RequestMapping("/del")
    public AjaxMessage delete(int ids) {

        int i = appInfoService.delete(ids);

        return new AjaxMessage(true, "删除成功");
    }

    //更新
    @RequestMapping("/update")
    public AjaxMessage update(AppInfo appInfo) {

        int i = appInfoService.update(appInfo);

        return new AjaxMessage(true, "更新成功");
    }

}